#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

import os
import sys


def validate(file):
    infile = open(file, "rb")
    check = "-1"

    l3 = ""

    for line in infile:
        line = line.rstrip()
        if not line == "":
            l3 = line
    infile.close()
    if l3.find("%%EOF") == -1:
        check = file
    return check


if __name__ == "__main__":
    list = os.listdir(sys.argv[1])
    parent = os.path.abspath(sys.argv[1])
    worknumber=os.path.basename(parent).split("_")[-1]
    outfile=open("pmc_pdf_worklist_invalid"+worknumber+".list","w")
    for file in list:
        if os.path.isfile(parent + "/" + file) and file[-3:] == "pdf":
            result=validate(parent + "/" + file)
            if not result=="-1":
                result=result[result.find("/PMC")+4:-4]
                outfile.write(result+"\n")
                outfile.flush()
    outfile.close()







